package com.senyint.sys.utils.quartz;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.Trigger.TriggerState;
import org.quartz.TriggerKey;
import org.quartz.impl.matchers.GroupMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Quartz 工具 {@code QuartzJobUtils}类
 * <p>
 * 
 * @author NaiFei Wang, wangnaifei@senyint.com
 * @date 2016年3月30日 上午11:14:13
 * @version 1.0
 * @since Quartz2.*
 */
public class QuartzJobUtils {
	
	private static final Logger log = LoggerFactory.getLogger(QuartzJobUtils.class);


	/**
	 * 删除一个job
	 * <p>
	 * 
	 * @author NaiFei Wang, wangnaifei@senyint.com
	 * @date 2016年3月30日 下午4:26:34
	 * @version 1.0
	 * @param scheduler
	 * @param jobId
	 * @param jobGroup
	 * @throws SchedulerException
	 */
	public static void deleteJob(Scheduler scheduler, String jobId, String jobGroup) throws SchedulerException {
		log.debug("jobId:{}", jobId);
		log.debug("jobGroup:{}", jobGroup);
		JobKey jobKey = JobKey.jobKey(jobId, jobGroup);
		scheduler.deleteJob(jobKey);
	}

	/**
	 * 暂停一个job
	 * <p>
	 * 
	 * @author NaiFei Wang, wangnaifei@senyint.com
	 * @date 2016年3月30日 下午4:26:46
	 * @version 1.0
	 * @param scheduler
	 * @param jobId
	 * @param jobGroup
	 * @throws SchedulerException
	 */
	public static void pauseJob(Scheduler scheduler, String jobId, String jobGroup) throws SchedulerException {
		log.debug("jobId:{}", jobId);
		log.debug("jobGroup:{}", jobGroup);
		JobKey jobKey = JobKey.jobKey(jobId, jobGroup);
		scheduler.pauseJob(jobKey);
	}

	/**
	 * 全部暂停
	 * <p>
	 * 
	 * @author NaiFei Wang, wangnaifei@senyint.com
	 * @date 2016年3月30日 下午4:28:36
	 * @version 1.0
	 * @param scheduler
	 * @throws SchedulerException
	 */
	public void pauseAll(Scheduler scheduler) throws SchedulerException {
		scheduler.pauseAll();
	}

	/**
	 * 恢复一个job
	 * <p>
	 * 
	 * @author NaiFei Wang, wangnaifei@senyint.com
	 * @date 2016年3月30日 下午4:26:22
	 * @version 1.0
	 * @param scheduler
	 * @param jobId
	 * @param jobGroup
	 * @throws SchedulerException
	 */
	public void resumeJob(Scheduler scheduler, String jobId, String jobGroup) throws SchedulerException {
		log.debug("jobId:{}", jobId);
		log.debug("jobGroup:{}", jobGroup);
		JobKey jobKey = JobKey.jobKey(jobId, jobGroup);
		scheduler.resumeJob(jobKey);
	}

	/**
	 * 全部恢复
	 * <p>
	 * 
	 * @author NaiFei Wang, wangnaifei@senyint.com
	 * @date 2016年3月30日 下午4:28:33
	 * @version 1.0
	 * @param scheduler
	 * @throws SchedulerException
	 */
	public void resumeAll(Scheduler scheduler) throws SchedulerException {
		scheduler.resumeAll();
	}

	/**
	 * 立即执行job
	 * <p>
	 * 
	 * @author NaiFei Wang, wangnaifei@senyint.com
	 * @date 2016年3月30日 下午4:29:37
	 * @version 1.0
	 * @param scheduler
	 * @param jobId
	 * @param jobGroup
	 * @throws SchedulerException
	 */
	public void runJobNow(Scheduler scheduler, String jobId, String jobGroup) throws SchedulerException {
		log.debug("jobId:{}", jobId);
		log.debug("jobGroup:{}", jobGroup);
		JobKey jobKey = JobKey.jobKey(jobId, jobGroup);
		scheduler.triggerJob(jobKey);
	}

	/**
	 * 获取JobDetail
	 * <p>
	 * 
	 * @author NaiFei Wang, wangnaifei@senyint.com
	 * @date 2016年3月30日 下午4:43:01
	 * @version 1.0
	 * @param scheduler
	 * @param jobId
	 * @param jobGroup
	 * @return
	 * @throws SchedulerException
	 */
	public static JobDetail getJobDetail(Scheduler scheduler, String jobId, String jobGroup) throws SchedulerException {
		log.debug("jobId:{}", jobId);
		log.debug("jobGroup:{}", jobGroup);
		JobKey jobKey = JobKey.jobKey(jobId, jobGroup);
		return scheduler.getJobDetail(jobKey);
	}

	/**
	 * 获取job状态
	 * <p>
	 * 
	 * @author NaiFei Wang, wangnaifei@senyint.com
	 * @date 2016年3月30日 下午4:46:15
	 * @version 1.0
	 * @param scheduler
	 * @param triggerId
	 * @param triggerGroup
	 * @return
	 * @throws SchedulerException
	 */
	public static TriggerState getJobDetailState(Scheduler scheduler, String triggerId, String triggerGroup) throws SchedulerException {
		log.debug("triggerId:{}", triggerId);
		log.debug("triggerGroup:{}", triggerGroup);
		TriggerKey triggerKey = TriggerKey.triggerKey(triggerId, triggerGroup);
		return scheduler.getTriggerState(triggerKey);
	}

	/**
	 * 获取所有计划中的任务列表
	 * <p>
	 * 
	 * @author NaiFei Wang, wangnaifei@senyint.com
	 * @date 2016年3月30日 下午6:24:27
	 * @version 1.0
	 * @param scheduler
	 * @return
	 * @throws SchedulerException
	 */
	public List<? extends Trigger> loadAllJob(Scheduler scheduler) throws SchedulerException {
		GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
		Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
		List<Trigger> jobList = new ArrayList<Trigger>();
		for (JobKey jobKey : jobKeys) {
			List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
			for (Trigger trigger : triggers) {
				jobList.add(trigger);
			}
		}
		return jobList;
	}

	/**
	 * 所有正在运行的job
	 * 
	 * @return
	 * @throws SchedulerException
	 */
	public List<? extends Trigger> loadRunningJob(Scheduler scheduler) throws SchedulerException {
		List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
		List<Trigger> jobList = new ArrayList<Trigger>(executingJobs.size());
		for (JobExecutionContext executingJob : executingJobs) {
			Trigger trigger = executingJob.getTrigger();
			TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
			// 状态是正常 并且 之前已经启动过 （没有运行中的状态）---多次启动 状态什么变化过程？？TODO
			if (triggerState.equals(TriggerState.NORMAL) && trigger.getPreviousFireTime().before(new Date())) {
				jobList.add(trigger);
			}

		}
		return jobList;
	}
}
